package com.jh.paymentchannel.service;

import cn.jh.common.utils.CommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestClientException;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@EnableAutoConfiguration
public class RepaymentService {

	private static final Logger log = LoggerFactory.getLogger(RepaymentService.class);

	@Autowired
	private PaymentChannelFactory factory;


	/**
	 * 代还业务，查询通道是否要先绑卡操作
	 * resp_code :
	 *   =  CommonConstants.STATUS_VALID 卡已经绑定并valid, 或通道不用绑卡直接返回valid
	 *      CommonConstants.STATUS_INVALID 需要绑卡操作, 前端将跳转到银行绑卡收银台
	 *      CommonConstants.FALIED 请求出错
	 *      CommonConstants.WAIT_CHECK 绑定正在处理中
	 * */
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/creditcard/verify/bind/card")
	public @ResponseBody Object verifyBindCardRequest(HttpServletRequest request,
											 @RequestParam(value = "channelTag") String channelTag,
											 @RequestParam(value = "userId") String userId,
											 @RequestParam(value = "creditCardNumber") String creditCardNumber,
											 @RequestParam(value = "brandId", required = false, defaultValue = "-1") String brandId) {
		Map<String, String> map = new HashMap<String, String>();

		log.info("=== verify bind creditcard ===" + channelTag);
		log.info("userId=====" + userId);

		RepaymentRequest repaymentRequest;
		try {


			repaymentRequest = factory.getRepaymentChannelRequest(channelTag);
		} catch (RestClientException e1) {
			e1.printStackTrace();
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲.网络出错了哦,臣妾已经尽力了,请重试~");
			return map;
		}

		try {
			map = repaymentRequest.verifyBindCardRequest(request, channelTag, userId, creditCardNumber, brandId);
			log.info("verifyBindCardRequest result====" + map);

			if (CommonConstants.STATUS_INVALID.equals(map.get(CommonConstants.RESP_CODE))) {
				log.info("绑卡状态 - 未绑定 进入授权 :" + map.get(CommonConstants.RESP_MESSAGE));
				map.put(CommonConstants.RESP_MESSAGE, "首次使用需进行绑卡授权,点击确定进入授权页面!");

			}else if (CommonConstants.STATUS_VALID.equals(map.get(CommonConstants.RESP_CODE))) {
				log.info("绑卡状态 = 已授权OK :" + map.get(CommonConstants.RESP_MESSAGE));
				map.put(CommonConstants.RESP_MESSAGE, "绑卡授权正常");

			}else if (CommonConstants.WAIT_CHECK.equals(map.get(CommonConstants.RESP_CODE))) {
				log.info("绑卡状态 = 授权处理中.. :" + map.get(CommonConstants.RESP_MESSAGE));
				map.put(CommonConstants.RESP_MESSAGE, "绑卡授权处理中, 请稍后在试");

			}else if (CommonConstants.FALIED.equals(map.get(CommonConstants.RESP_CODE))) {
				log.error("绑卡授权检测失败 :" + map.get(CommonConstants.RESP_MESSAGE));
				map.put(CommonConstants.RESP_MESSAGE, "绑卡授权检测失败, 请稍后在试");
			}
		} catch (Exception e) {
			log.error("==========verify bind creditcard请求通道异常===========" + e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲.网络出错了哦,臣妾已经尽力了,请重试~");
			return map;
		}

		return map;
	}





}
